import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicColumn, BasicTable, FormSchema, useTable } from "@/components/Table";
import "./index.less";
import { useTranslation } from "react-i18next";
import { WarehouseApi } from "@/api/erp/stock/warehouse";
import { DocMgmtApi } from "@/api/erp/purchase/docMgmt";
import { useEffect, useState } from "react";
import { handleTree } from "@/utils/tree";
import * as UserApi from "@/api/system/user";
import { listSimpleDept } from "@/api/system/dept";
const newAdd = (props: any) => {
	const en_vi = "drugStorage.purchasingManagement.other";
	const [treeData, setTreeData] = useState([]);
	const { t } = useTranslation();
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		console.log("data>>>>>>>>>>>>>>>", data);
	});
	const schemas: FormSchema[] = [
		{
			label: t(`${en_vi}.deptName`),
			field: "deptId",
			component: "TreeSelect",
			componentProps: {
				treeData,
				fieldNames: {
					value: "id",
					label: "name"
				}
			}
		},
		{
			label: t(`${en_vi}.receiverId`),
			component: "ApiSelect",
			field: "receiverId",
			componentProps: {
				api: UserApi.getListSimpleUsers,
				labelField: "nickname",
				valueField: "id",
				immediate: false
			}
		},
		{
			label: t("drugStorage.inventoryManagement.drugInventory.warehouseName"),
			field: "whsId",
			component: "ApiSelect",
			componentProps: {
				api: WarehouseApi.getWarehouseSimpleList1,
				labelField: "whsName",
				valueField: "whsId"
			}
		}
	];
	const columns: BasicColumn[] = [
		{
			title: t(`${en_vi}.yearDate`),
			dataIndex: "yearDate"
		},
		{
			title: t(`${en_vi}.receiptDate`),
			dataIndex: "receiptDate"
		},
		{
			title: t(`${en_vi}.deptName`),
			dataIndex: "deptName"
		},
		{
			title: t(`${en_vi}.receiptType`),
			dataIndex: "receiptType"
		},
		{
			title: t(`${en_vi}.receiptNo`),
			dataIndex: "receiptNo"
		},
		{
			title: t(`${en_vi}.printType`),
			dataIndex: "printType"
		},
		{
			title: t(`${en_vi}.whsId`),
			dataIndex: "whsId"
		},
		{
			title: t(`${en_vi}.bizId`),
			dataIndex: "bizId"
		},
		{
			title: t(`${en_vi}.qty`),
			dataIndex: "qty"
		},
		{
			title: t(`${en_vi}.fundingId`),
			dataIndex: "fundingId"
		},
		{
			title: t(`${en_vi}.printCount`),
			dataIndex: "printCount"
		},
		{
			title: t(`${en_vi}.receiverId`),
			dataIndex: "receiverId"
		}
	];
	const [registerTable, { getSelectRows }] = useTable({
		api: DocMgmtApi.getDocMgmtPage,
		columns,
		rowSelection: true,
		formConfig: {
			labelWidth: 120,
			schemas,
			showActionButtonGroup: true,
			showAdvancedButton: false,
			baseColProps: { span: 8 },
			actionColOptions: { span: 24 }
		},
		bordered: true,
		useSearchForm: true
	});
	const handleSubmit = async () => {
		const orderItem = getSelectRows();
		props.setReceiptNo(orderItem[0]?.receiptNo);
		closeDrawer();
	};
	useEffect(() => {
		const getTreeData = async () => {
			const data = await listSimpleDept();
			let root: any = { id: 0, name: "部门分类", children: [] };
			root.children = handleTree(data, "id", "parentId");
			let newTreeData = treeData;
			newTreeData.push(root);
			setTreeData(newTreeData);
		};
		getTreeData();
	}, []);
	return (
		<BasicDrawer
			size="large"
			{...props}
			title={t("drugStorage.selectPurchaseOrder")}
			register={registerDrawer}
			ok={handleSubmit}
			showFooter={true}
		>
			<BasicTable register={registerTable}></BasicTable>
		</BasicDrawer>
	);
};
export default newAdd;
